package com.harmontronics.hanma.paas.paas.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmActionAppService;
import com.harmontronics.hanma.paas.paas.dto.iam.output.HmActionResult;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmActionCreateInput;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmActionUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmAction;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 菜单按钮
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-07 10:36:37
 */
@RestController
@RequestMapping("/api/hmaction")
@Api(tags = {"菜单按钮"})
public class HmActionController {
    @Autowired
    private HmActionAppService hmActionappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmActionResult>> list(@RequestParam Long appId) {
        List<HmAction> hmActionList = hmActionappService.getList(appId);
        return ResponseEntity.ok(Convert.toList(HmActionResult.class, hmActionList));
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("详情")
    public ResponseEntity<HmActionResult> info(@PathVariable("id") Integer id) {
        HmAction hmAction =hmActionappService.findById(id);
            HmActionResult result = BeanUtil.toBean(hmAction,HmActionResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmActionCreateInput hmActionInput,BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmAction hmAction =BeanUtil.toBean(hmActionInput,HmAction.class);
        hmActionappService.create(hmAction);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmActionUpdateInput hmActionInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmAction hmAction =BeanUtil.toBean(hmActionInput,HmAction.class);
            hmActionappService.update(hmAction);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmActionappService.remove(Arrays.asList(ids));
        return ResponseEntity.ok();
    }

}
